<?php

/**
 *  @author  Lynn Cho <lynncho@live.com>
 *  @createTime  2016-9-13 14:10:41
 *  @copyright  shewen technology
 *  @descrition  
 */
class ServiceModel extends BaseModel {

    public function __construct() {
        parent::__construct();
        $this->load->library('manager/Permission');
        $this->load->model('ServiceGroupModel');
    }

    public function getService($account, $name, $group, $stime, $etime, $page = 0, $rows = 0) {
        if (!empty($account))
            $where['account'] = $account;

        if (!empty($name)) {
            $where['name'] = $name;
        }
        if (!empty($where))
            $this->db->where($where);
        if (!empty($stime) && !empty($etime))
            $this->db->where("last_login BETWEEN '$stime' AND '$etime'");
        if (!empty($group[0]))
            $this->db->where_in('groupid', $group);
        $re['total'] = $this->db->where('delete', 0)->get('service_user')->num_rows();

        if (!empty($where))
            $this->db->where($where);
        if (!empty($stime) && !empty($etime))
            $this->db->where("last_login BETWEEN '$stime' AND '$etime'");
        if (!empty($group[0]))
            $this->db->where_in('groupid', $group);
        $re['rows'] = $this->db
                ->where('delete', 0)
                ->select(array('id', 'name', 'account', 'groupid', 'phone', 'email', 'offer', 'last_login'))
                ->limit($rows, $rows * ($page - 1))
                ->order_by('groupid', 'DESC')
                ->get('service_user')
                ->result_array();

        $this->load->model('ServiceGroupModel');
        $groupList = $this->ServiceGroupModel->getRealGroup();
        foreach ($re['rows'] as $key => $val) {
            foreach ($groupList as $val2) {
                if ($val['groupid'] == $val2['id']) {
                    $re['rows'][$key]['group_name'] = $val2['name'];
                    $re['rows'][$key]['group_account'] = $val2['account'];
                    break;
                }
            }
        }

        return $re;
    }

    public function getGroup($account, $name, $page = 0, $rows = 0) {
        if (!empty($account))
            $where['account'] = $account;

        if (!empty($name)) {
            $where['name'] = $name;
        }
        if (!empty($where))
            $this->db->where($where);
        $re['total'] = $this->db->where('delete', 0)->get('service_group')->num_rows();

        if (!empty($where))
            $this->db->where($where);
        $re['rows'] = $this->db
                ->where('delete', 0)
                ->limit($rows, $rows * ($page - 1))
                ->order_by('id', 'DESC')
                ->get('service_group')
                ->result_array();

        foreach ($re['rows'] as $key => $val) {
            $re['rows'][$key]['servicenum'] = $this->db->where('groupid', $val['id'])->get('service_user')->num_rows();
        }

        return $re;
    }

    public function getChatLog($account, $name, $stime, $etime, $page = 0, $rows = 0) {
        if (!empty($account))
            $where['service_account'] = $account;

        if (!empty($name)) {
            $reid = $this->db->select('id')->where('name', $name)->get('service_user')->result_array();
            foreach ($reid as $key => $val) {
                $idArr[$key] = $val['id'];
            }
            unset($reid);
            $this->db->where_in('service_id', $idArr);
        }
        if (!empty($stime) && !empty($etime))
            $this->db->where("time BETWEEN '$stime' AND '$etime'");
        if (!empty($where))
            $this->db->where($where);
        $re['total'] = $this->db->get('log')->num_rows();

        if (!empty($where))
            $this->db->where($where);
        if (!empty($stime) && !empty($etime))
            $this->db->where("time BETWEEN '$stime' AND '$etime'");
        if (!empty($idArr)) {
            $this->db->where_in('service_id', $idArr);
        }
        $re['rows'] = $this->db
                ->select(array('id', 'ip', 'cid', 'content', 'time', 'sid', 'service_account'))
                ->limit($rows, $rows * ($page - 1))
                ->order_by('time', 'DESC')
                ->get('log')
                ->result_array();

        return $re;
    }

    public function getDownloadChat($account, $name, $stime, $etime) {
        if (!empty($name)) {
            $reAccount = $this->db->select('account')->where('name', $name)->get('service_user')->result_array();
            foreach ($reAccount as $key => $val) {
                $accountArr[$key] = $val['account'];
            }
            unset($reAccount);
            $this->db->where_in('service_account', $accountArr);
        }
        if (!empty($account))
            $where['service_account'] = $account;
        if (!empty($where))
            $this->db->where($where);
        if (!empty($stime) && !empty($etime))
            $this->db->where("time BETWEEN '$stime' AND '$etime'");
        $re = $this->db
                ->select(array('ip', 'cid', 'content', 'time', 'sid', 'service_account'))
                ->order_by('time', 'DESC')
                ->get('log')
                ->result_array();
        return $re;
    }

    public function addService($array) {
        $array['pwd'] = $this->permission->customMD5($array['pwd']);
        $array['id'] = $this->createID('service_user', 'id');
        if (empty($array['id']))
            return;
        if (empty($array['groupid'])) {
            return;
        } else {
            if (!self::isRealGroup($array['groupid'])) {
                return;
            } else
                $array['levelid'] = $this->ServiceGroupModel->getLevel($array['groupid']);
        }

        $this->db->trans_begin();
        $this->db->insert('service_user', $array);
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    private function isRealGroup($groupid) {
        $realGroup = $this->ServiceGroupModel->getRealGroup();
        foreach ($realGroup as $val) {
            if ($val['id'] == $groupid) {
                return true;
            }
        }
        return false;
    }

    public function editService($array) {
        if (empty($array['pwd'])) {
            unset($array['pwd']);
        } else {
            $array['pwd'] = $this->permission->customMD5($array['pwd']);
        }
        if (empty($array['id'])) {
            return;
        }
        if (empty($array['groupid'])) {
            return;
        } else {
            if (!self::isRealGroup($array['groupid'])) {
                return;
            } else
                $array['levelid'] = $this->ServiceGroupModel->getLevel($array['groupid']);
        }

        $this->db->trans_begin();
        $this->db->where('id', $array['id'])->update('service_user', $array);
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    public function deleteService($id) {
        if (is_numeric($id)) {
            $this->db->trans_begin();
            $this->db->where('id', $id)->update('service_user', array('delete' => 1));
            if ($this->db->trans_status() === FALSE) {
                $this->db->trans_rollback();
                return false;
            } else {
                $this->db->trans_commit();
                return true;
            }
        } else {
            return 301;
        }
    }

    public function getGroupTree() {
        return $this->ServiceGroupModel->getGroupTree();
    }

}
